package org.example.curriculum_design_6_16.mapper;

import org.apache.ibatis.annotations.*;
import org.example.curriculum_design_6_16.entity.pojo.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.time.LocalDateTime;

/**
* @author 郑凯
* @description 针对表【user】的数据库操作Mapper
* @createDate 2025-06-16 13:21:18
* @Entity org.example.curriculum_design_6_16.entity.pojo.User
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
    /**
     * 检查用户名是否存在
     * @param username 用户名
     * @return 是否存在
     */
    @Select("SELECT COUNT(*) FROM user WHERE username = #{username}")
    boolean existsByUsername(@Param("username") String username);

    /**
     * 检查邮箱是否存在
     * @param email 邮箱
     * @return 是否存在
     */
    @Select("SELECT COUNT(*) FROM user WHERE email = #{email}")
    boolean existsByEmail(@Param("email") String email);

    /**
     * 根据用户名查找用户
     * @param username 用户名
     * @return 用户实体
     */
    @Select("SELECT * FROM user WHERE username = #{username}")
    User findByUsername(@Param("username") String username);

    @Insert("INSERT INTO user (user_id, username, password_hash, email, phone, real_name, created_at, is_active) " +
            "VALUES (#{userId}, #{username}, #{passwordHash}, #{email}, #{phone}, #{realName}, #{createdAt}, #{isActive})")
    int insert(User user);


    @Update("UPDATE user SET " +
            "last_login = #{loginTime} " +
            "WHERE user_id = #{userId}")
    void updateLoginInfo(
            @Param("userId") String userId,
            @Param("loginTime") LocalDateTime loginTime
    );
}




